// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "order_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ]
  },
  "huoping_id": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "product_code": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ]
  },
  "product_name": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ]
  },
  "specification": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ]
  },
  "unit": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ]
  },
  "yf_quantity": {
    "rules": [
      {
        "format": "decimal"
      }
    ]
  },
  "quantity": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "decimal"
      }
    ]
  },
  "moq": {
    "rules": [
      {
        "format": "decimal"
      }
    ]
  },
  "unit_price_excl_tax": {
    "rules": [
      {
        "format": "decimal"
      }
    ]
  },
  "tax_rate": {
    "rules": [
      {
        "format": "decimal"
      }
    ]
  },
  "tax_amount": {
    "rules": [
      {
        "format": "decimal"
      }
    ]
  },
  "amount": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "decimal"
      }
    ]
  },
  "quantity_sold": {
    "rules": [
      {
        "format": "decimal"
      }
    ]
  },
  "gift": {
    "rules": [
      {
        "format": "bool"
      }
    ]
  },
  "delivery_date": {
    "rules": [
      {
        "format": "timestamp"
      }
    ]
  },
  "order_line_number": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "internal_special_note": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "supplier_PN": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "goods_desc": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "version": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "cleaning_type": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "qx_price": {
    "rules": [
      {
        "format": "decimal"
      }
    ]
  },
  "price_excl_tax": {
    "rules": [
      {
        "format": "decimal"
      }
    ]
  },
  "position": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "additional_items": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "is_kp": {
    "rules": [
      {
        "format": "bool"
      }
    ]
  },
  "kp_num": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "kp_date": {
    "rules": [
      {
        "format": "timestamp"
      }
    ]
  },
  "status": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "配货",
            "value": 0
          },
          {
            "text": "清洗中",
            "value": 1
          },
          {
            "text": "已发",
            "value": 2
          },
          {
            "text": "终止",
            "value": 3
          }
        ]
      }
    ],
    "defaultValue": 0
  }
}

const enumConverter = {
  "status_valuetotext": {
    "0": "配货",
    "1": "清洗中",
    "2": "已发",
    "3": "终止"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
